from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import ListAPIView
from rest_framework.decorators import action
from rest_framework.response import Response
from .serializers import *
from question.utils import get_token_user


class CityHotListView(ListAPIView):

    queryset = City.objects.filter(ishot=1)
    serializer_class = CityhotSerializers


class RecruitsSearchViewSet(ModelViewSet):
    queryset = Recruit.objects.filter(state=1)
    serializer_class = RecruitSerializers

    # 最新
    @action(methods=['get'], detail=False)
    def latest(self, request):
        return self.list(request)

    # 推荐
    @action(methods=['get'], detail=False)
    def recommend(self, request):
        self.queryset = self.queryset.order_by('-visits')[:6]
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)

    # 搜索职位
    @action(methods=['post'], detail=False, url_path='city/keyword')
    def search(self, request):
        city = self.request.data.get('cityname')
        keyword = self.request.data.get('keyword')
        filters = {'jobname__contains': keyword, 'city': city}
        if not keyword and not city:
            filters = {}
        elif not keyword and city:
            filters.pop('jobname__contains')
        elif not city and keyword:
            filters.pop('city')
        self.queryset = self.queryset.filter(**filters)
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)


class RecruitsViewSet(ModelViewSet):
    """职位详情相关"""
    queryset = Recruit.objects.filter(state=1)
    serializer_class = RecruitDetailSerializers

    @action(methods=['put'], detail=True)
    def visit(self, request, pk):
        recruit = self.get_object()
        recruit.visits += 1
        recruit.save()
        return Response({'success':True, 'message':'ok'})

    def __collect(self, request, pk):
        user = get_token_user(request)
        recruit = self.get_object()
        ru = recruit.users.all().filter(id=user.id)
        if ru:
            recruit.users.remove(user)
        else:
            recruit.users.add(user)
        return Response({'success': True, 'message': 'ok'})

    # 收藏
    @action(methods=['post'], detail=True)
    def collect(self, request, pk):
        return self.__collect(request, pk)

    # 取消收藏
    @action(methods=['post'], detail=True)
    def cancelcollect(self, request, pk):
        return self.__collect(request, pk)


class EnterpriseSearchViewSet(ModelViewSet):
    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseSimpleSerializers

    # 最热企业
    @action(methods=['get'], detail=False)
    def hotlist(self, request):
        self.queryset = self.queryset.order_by('-visits')[:6]
        serializer = self.get_serializer(self.queryset, many=True)
        return Response(serializer.data)


class EnterpriseViewSet(ModelViewSet):
    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseSerializers

    # 访问次数
    @action(methods=['put'], detail=True)
    def visit(self, request, pk):
        enterprise = self.get_object()
        enterprise.visits += 1
        enterprise.save()
        return Response({'success': True, 'message': 'ok'})

    def __collect(self, request, pk):
        user = get_token_user(request)
        enterprise = self.get_object()
        eu = enterprise.users.all().filter(id=user.id)
        if eu:
            enterprise.users.remove(user)
        else:
            enterprise.users.add(user)
        return Response({'success': True, 'message': 'ok'})

    # 收藏
    @action(methods=['post'], detail=True)
    def collect(self, request, pk):
        return self.__collect(request, pk)

    # 取消收藏
    @action(methods=['post'], detail=True)
    def cancelcollect(self, request, pk):
        return self.__collect(request, pk)